/**
 * Created by jjl on 2017/7/10.
 */


var express = require('express');
var router = express.Router();
var process = require('child_process');
var os = require('os');
var http = require('http');
var config = require('../routes/config');
var nodemailer = require('nodemailer');
var https = require('https');
var qs = require('querystring');
var mysql = require('mysql');
router.post('/', function(req, res, next) {
    if (!req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    console.log(JSON.stringify(req.body));
    var post_data = "";
    var content = "";
    var s_ip="";
    var s_port=0;
    try{
        var oo = (req.body);
        var cc = {
            content:oo.CONTENT,
            type:2,
        };
        post_data = JSON.stringify(cc);
        content = oo.CONTROL;
        s_ip = oo.S_IP;
        s_port = parseInt(oo.S_PORT);
    }catch(e)
    {
        res.write("error");
        console.log("Error:" + e);
        res.end();
        return;
    }
    console.log("post:" + post_data);
    var options = {
        path: '/'+content,
        method: 'POST',
        host: s_ip,
        port: s_port,
        headers: {
            'Content-Type':'application/json',
            'Content-Length':  Buffer.byteLength(post_data,'utf8')
        }
    };
    console.log(JSON.stringify(options) + ":" + post_data.length);
    var reqHttps = http.request(options, function(resHttps) {
        console.log("statusCode: ", resHttps.statusCode);
        console.log("headers: ", resHttps.headers);

        resHttps.setEncoding('utf8');
        resHttps.on('data', function(body1) {
            console.log("body:"+body1);
            res.write((body1));
            res.end();

        });
    });
    reqHttps.write(post_data);
    reqHttps.end();
    reqHttps.on('error', function(e) {
        var o ={
            code:-1,
            message:"失敗",
        }
        res.write(o);
        res.end();
    });

    //res.write("SUCEESS!!!!!!!!!");
    //res.end();
});


router.post('/mail', function(req, res, next) {
    if (!req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    console.log(JSON.stringify(req.body));
    var post_data = "";
    var content = "";
    var s_ip="";
    var s_port=0;
    try{
        var oo = (req.body);
        post_data = oo.CONTENT;
        content = "mail";
        s_ip = oo.S_IP;
        s_port = parseInt(oo.S_PORT);
    }catch(e)
    {
        res.write("error");
        console.log("Error:" + e);
        res.end();
        return;
    }
    console.log("post:" + post_data);
    var options = {
        path: '/'+content,
        method: 'POST',
        host: s_ip,
        port: s_port,
        headers: {
            'Content-Type':'application/json',
            'Content-Length':  Buffer.byteLength(post_data,'utf8')
        }
    };
    console.log(JSON.stringify(options) + ":" + post_data.length);
    var reqHttps = http.request(options, function(resHttps) {
        console.log("statusCode: ", resHttps.statusCode);
        console.log("headers: ", resHttps.headers);

        resHttps.setEncoding('utf8');
        resHttps.on('data', function(body1) {
            console.log("body:"+body1);
            var oo = JSON.parse(body1);
            res.render('error',{
                links:config.linkmenu,
                index:config.link.INDEX.link,
                login:req.session.login,
                message:oo.message,
            });

        });
    });
    reqHttps.write(post_data);
    reqHttps.end();
    reqHttps.on('error', function(e) {
        res.render('error',{
            links:config.linkmenu,
            index:config.link.INDEX.link,
            login:req.session.login,
            message:"出錯了",
        });
    });

    //res.write("SUCEESS!!!!!!!!!");
    //res.end();
});


router.post('/gm', function(req, res, next) {
    if (!req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    console.log(JSON.stringify(req.body));
    var post_data = "";
    var content = "";
    var s_ip="";
    var s_port=0;
    try{
        var oo = (req.body);
        post_data = oo.CONTENT;
        content = "sendgmcmd";
        s_ip = oo.S_IP;
        s_port = parseInt(oo.S_PORT);
    }catch(e)
    {
        res.write("error");
        console.log("Error:" + e);
        res.end();
        return;
    }
    console.log("post:" + post_data);
    var options = {
        path: '/'+content,
        method: 'POST',
        host: s_ip,
        port: s_port,
        headers: {
            'Content-Type':'application/json',
            'Content-Length':  Buffer.byteLength(post_data,'utf8')
        }
    };
    console.log(JSON.stringify(options) + ":" + post_data.length);
    var reqHttps = http.request(options, function(resHttps) {
        console.log("statusCode: ", resHttps.statusCode);
        console.log("headers: ", resHttps.headers);

        resHttps.setEncoding('utf8');
        resHttps.on('data', function(body1) {
            console.log("body:"+body1);
            var oo = JSON.parse(body1);
            res.render('error',{
                links:config.linkmenu,
                index:config.link.INDEX.link,
                login:req.session.login,
                message:oo.message,
            });

        });
    });
    reqHttps.write(post_data);
    reqHttps.end();
    reqHttps.on('error', function(e) {
		console.log(e);
        res.render('error',{
            links:config.linkmenu,
            index:config.link.INDEX.link,
            login:req.session.login,
            message:"出錯了",
        });
    });

    //res.write("SUCEESS!!!!!!!!!");
    //res.end();
});

module.exports = router;